package com.rafahead.bloquinho;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MatchListFragment extends ListFragment {
	
    // The container Activity must implement this interface so the frag can deliver messages
	public interface MatchSelectedCallBack{
		public void onMatchSelected(int position);
	}
	
	MatchSelectedCallBack mCallBack;

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

        // Notify the parent activity of selected item
		mCallBack.onMatchSelected(position);

		// Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
		
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallBack = (MatchSelectedCallBack) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()+ " must implement MatchSelectedCallBack");
        }
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

	}
	
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
/*        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }*/
    }

	@Override
	public void onStart() {
		super.onStart();
	       // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.matchdetail_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }		
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
        // We need to use a different list item layout for devices older than Honeycomb
		int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
		
		setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));

	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.match_list_menu, menu);
		super.onCreateOptionsMenu(menu,inflater);		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_add_match:
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

}
